/*
Copyright 2009 Johan Maasing, Mikael Forsberg

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.google.code.pj2r.sample.simple.resources;

import java.util.Iterator;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.code.pj2r.DataAccessException;
import com.google.code.pj2r.ResourceException;
import com.google.code.pj2r.sample.simple.dao.StuffDAO;
import com.google.code.pj2r.sample.simple.dao.StuffEntity;
import com.google.code.pj2r.util.resource.JSONResource;

public class Stuff extends JSONResource {

    /**
     * This is used when handling POST data for the create case.
     */
    private String stuff;

    /**
     * Must have a public no-args constructor.
     */
    public Stuff() {
    }

    /**
     * Used to get the POSTed data when creating new Stuff.
     * @return
     */
    public String getStuff() {
        return stuff;
    }

    /**
     * Set by the framework to inject POSTed data.
     * @param stuff The POSTed parameter.
     */
    public void setStuff(String stuff) {
        this.stuff = stuff;
    }

    /**
     * We should create a new resource.
     * @return The new ID for the resource.
     * @throws ResourceException If creating the resource fails for some reason.
     */
    public String create() throws DataAccessException {
        // Create a persistent entity
        StuffEntity entity = new StuffEntity();

        // Retrieve the POSTed data and set it on the entity. The POST data is
        // retrieved from the HttpServletRequest and injected by the setXXX
        // methods on this class.
        entity.setValue(getStuff());

        // Persist the new entity
        StuffDAO.getInstance().add(entity);

        // Return the new Entity ID so that the Location header an be created.
        return entity.getId();
    }


    /**
     * Delete a resource
     */
	public void delete(String id) {
		StuffDAO.getInstance().delete(id);
	}

    /**
     * Create the JSON result object for the given ID
     * @param id
     * @return
     * @throws ResourceException
     * @throws JSONException
     */
    public JSONObject getObject(String id) throws DataAccessException, JSONException {

        StuffEntity entity = StuffDAO.getInstance().get(id);
        return toJSON(entity);
    }

    /**
     * Get a list of all Stuff
     * @return
     * @throws ResourceException
     * @throws JSONException
     */
    public JSONArray listArray() throws DataAccessException, JSONException {
        List result = StuffDAO.getInstance().getAll();
        JSONArray json = new JSONArray();
        for (Iterator i = result.iterator(); i.hasNext();) {
            StuffEntity entity = (StuffEntity) i.next();
            json.put(toJSON(entity));
        }
        return json;
    }

    private JSONObject toJSON(StuffEntity entity) throws JSONException {
        if (entity != null) {
            JSONObject result = new JSONObject();
            // Hand crafted JSON object.
            result.put("id", entity.getId());
            result.put("stuff", entity.getValue());
            return result;
        }
        return null;
    }

	public void update(String id) throws DataAccessException {
		StuffDAO dao = StuffDAO.getInstance();
        StuffEntity entity = dao.get(id);
		entity.setValue(getStuff());
		// Since the dao is actually a fake it should be enough to mutate
		// the object to make it "persistant"
	}
}
